﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using De.LarsHildebrandt.WpfControls.WpfSimpleChart;
using MyFinanceManager.Objects;

namespace MyFinanceManager.PC
{
    /// <summary>
    /// Interaction logic for ucViewCategoriesGraph.xaml
    /// </summary>
    public partial class ucViewCategoriesGraph : UserControl
    {
        #region Fields
        private ObservableCollection<ChartLegendItem> pieLegendExp = new ObservableCollection<ChartLegendItem>();
        private ObservableCollection<ChartRow> pieRowsExp = new ObservableCollection<ChartRow>();
        private ObservableCollection<ChartLegendItem> pieLegendEar = new ObservableCollection<ChartLegendItem>();
        private ObservableCollection<ChartRow> pieRowsEar = new ObservableCollection<ChartRow>();
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the pie rows for expenses
        /// </summary>
        /// <value>The pie rows for expenses</value>
        public ObservableCollection<ChartRow> PieRowsExp
        {
            get { return pieRowsExp; }
            set { pieRowsExp = value; }
        }
        /// <summary>
        /// Gets or sets the pie legend for expenses.
        /// </summary>
        /// <value>The pie legend for expenses.</value>
        public ObservableCollection<ChartLegendItem> PieLegendExp
        {
            get { return pieLegendExp; }
            set { pieLegendExp = value; }
        }

        /// <summary>
        /// Gets or sets the pie rows for earnings.
        /// </summary>
        /// <value>The pie rows for earnings.</value>
        public ObservableCollection<ChartRow> PieRowsEar
        {
            get { return pieRowsEar; }
            set { pieRowsEar = value; }
        }
        /// <summary>
        /// Gets or sets the pie legend for earnings.
        /// </summary>
        /// <value>The pie legend for earnings.</value>
        public ObservableCollection<ChartLegendItem> PieLegendEar
        {
            get { return pieLegendEar; }
            set { pieLegendEar = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="ucViewCategoriesGraph"/> class.
        /// </summary>
        public ucViewCategoriesGraph()
        {
            InitializeComponent();
            PreparePieChartExp();
            PreparePieChartEar();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Prepares the pie chart for earnings.
        /// </summary>
        private void PreparePieChartEar()
        {
            PreparePieChart(RepositoryCenter.Repositoy.EarningsCategorized, this.pieRowsEar, this.pieLegendEar, color.green);
        }

        /// <summary>
        /// Prepares the pie chart for expenses.
        /// </summary>
        private void PreparePieChartExp()
        {
            PreparePieChart(RepositoryCenter.Repositoy.ExpensesCategorized, this.pieRowsExp, this.pieLegendExp, color.red);
        }

        /// <summary>
        /// Prepares the pie chart.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="row">The row.</param>
        /// <param name="legend">The legend.</param>
        private void PreparePieChart(CatItem items, ObservableCollection<ChartRow> row, ObservableCollection<ChartLegendItem> legend, color colorKind)
        {
            row.Clear();
            legend.Clear();
            foreach (var item in items)
            {
                Color c = GetColor(colorKind);
                row.Add(new ChartRow(item.NameAndSaldoKum, item.SaldoKumuliert + 0.001, c));
                legend.Add(new ChartLegendItem(item.NameAndSaldoKum, c));
            }
        }

        enum color
        {
            green,
            red
        }

        private static Random gen = null;
        private static Color GetColor(color colorKind)
        {
            if (gen == null)
                gen = new Random(DateTime.Now.Millisecond);

            Color c = Colors.Violet;
            switch (colorKind)
            {
                case color.green:
                    c = Color.FromRgb((byte)gen.Next(255), (byte)255, (byte)0);
                    break;
                case color.red:
                    c = Color.FromRgb((byte)255, (byte)gen.Next(255), (byte)0);
                    break;
                default:
                    c = Color.FromRgb((byte)gen.Next(61), (byte)gen.Next(255), (byte)gen.Next(255));
                    break;
            }
            return c;
        }
        #endregion

        private void ReColorEarnings_Click(object sender, RoutedEventArgs e)
        {
            PreparePieChartEar();
        }

        private void ReColorExpenses_Click(object sender, RoutedEventArgs e)
        {
            PreparePieChartExp();
        }
    }
}